package kit.edu.lego.gruppe3.levels.labyrinth;

import kit.edu.lego.gruppe3.ComponentsRepository;
import kit.edu.lego.gruppe3.LegoMain;
import kit.edu.lego.gruppe3.levels.LevelSelector;
import kit.edu.lego.gruppe3.levels.LevelSelector.Level;
import kit.edu.lego.gruppe3.levels.sumpf.SumpfSolver;
import lejos.nxt.LightSensor;
import lejos.robotics.subsumption.Behavior;

public class LabyrinthEndReached implements Behavior{

	
	private LightSensor lightS = ComponentsRepository.getInstance().getLightSensor();

	@Override
	public void action()
	{
		// either sumpf or line folower
		ComponentsRepository.getInstance().getMiddleMotor().flt();

		if ( ! SumpfSolver.isLevelCleared())
		{
			LevelSelector.getInstance().setCurrentLevel( Level.sumpf );
			LegoMain.startSumpf();
		} else 
		{
			ComponentsRepository.getInstance().getRobotPilot().rotate(-40);
			ComponentsRepository.getInstance().getRobotPilot().travel(4.4f);
			while (ComponentsRepository.getInstance().getLightSensor().getLightValue() <
				ComponentsRepository.LIGHT_TRASHOLD){
				ComponentsRepository.getInstance().getRobotPilot().steer(15);
			}
			ComponentsRepository.getInstance().getRobotPilot().rotate(-20);
			LabyrinthSolver.setLevelCleared(true);
			LevelSelector.getInstance().setCurrentLevel( Level.line );
			LegoMain.startLine();
		}
		
	}

	@Override
	public void suppress()
	{

	}

	@Override
	public boolean takeControl()
	{
		return LevelSelector.getInstance().isLabyrinth() &&
			lightS.getLightValue() > ComponentsRepository.LIGHT_TRASHOLD;
	}

}
